<%

/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

%>

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="bw.news.*" %>

<%
    
    NewsManager manager = NewsManager.getInstance();

    int catId = Integer.parseInt( request.getParameter( "catId" ) );
    NewsCategory newsCategory = manager.getNewsCategoryForId( catId );

    String feedId = request.getParameter( "feedId" );

    String name = "";
    String src = "";
    String site = "";
    String refresh = null;
    String username = "";
    String password = "";
    int categoryId = newsCategory.getId();
    boolean isEditing = ( feedId != null );

    NewsSource source = null;

    Vector errors = new Vector();

    String action = request.getParameter( "action" );
    if ( isEditing && action == null )
    {
      NewsSource s = newsCategory.getNewsSourceForId( feedId );
      name = s.getName();
      src = s.getSrc();
      site = s.getSite();
      refresh = s.getRefreshStr();
      username = ( s.getUsername() == null ? "" : s.getUsername() );
      password = ( s.getPassword() == null ? "" : s.getPassword() );
    }
    else if ( action != null )
    {
      name = request.getParameter( "name" );
      src = request.getParameter( "src" );
      site = request.getParameter( "site" );
      refresh = request.getParameter( "refresh" );
      int refreshInt = Integer.parseInt( refresh );
      username = request.getParameter( "username" );
      password = request.getParameter( "password" );
      categoryId = Integer.parseInt( request.getParameter( "categoryId" ) );

      if ( name == null || name.length() == 0 )
      {
        errors.addElement( "Enter a name." );
      }

      if ( src == null || src.length() == 0 )
      {
        errors.addElement( "Enter a feed src." );
      }

      if ( errors.size() == 0 )
      {
        if ( isEditing ) 
        {
	  NewsSource s = newsCategory.getNewsSourceForId( feedId );
	  s.setName( name );
	  s.setSrc( src );
	  s.setSiteURL( site );
	  s.setRefreshMinutes( refreshInt );
	  if ( username != null && username.length() > 0 && 
	       password != null && password.length() > 0 ) 
	  {
	    s.setUsername( username );
	    s.setPassword( password );
	  }	

	  s.sync();

 	  if ( catId != categoryId ) 
          {
	    //we are changing the category for this feed.
	    //so delete the feed from this category, get the new cat and add it
	    manager.moveNewsSourceToCategory( s, newsCategory, categoryId );
  	    manager.syncChanges();	      
          }

	  String u = "editNewsCategory.jsp?catId=" + newsCategory.getId() + 
                         "&editedFeed=" + feedId;
	  if ( catId != categoryId ) { 
	    u += "&movedToCat=" + categoryId;
          }

	  response.sendRedirect( u );
        } 
	else
	{
      	  source = manager.newNewsSource( name, src, site, refreshInt );
	  if ( username != null && username.length() > 0 && 
	       password != null && password.length() > 0 ) 
	  {
	    source.setUsername( username );
	    source.setPassword( password );
	  }

	  manager.getNewsCategoryForId( categoryId ).addNewsSource( source );

	  manager.syncChanges();

          response.sendRedirect( "editNewsCategory.jsp?catId=" + categoryId + 
	                         "&addedFeed=" + source.getId() );
        }
      }
    }  

%>

<html>
<head>
<link rel=STYLESHEET type="text/css" href="style.jsp">
</head>

<body style="margin:0px;">

<div class="mainAddNews">

<%
   if ( errors.size() > 0 ) 
   {
%>
<div class="errors">
<% 
     for ( Enumeration e = errors.elements(); e.hasMoreElements(); ) { 
       String error = (String) e.nextElement();
%>
  <div class="errorMessage"><%= error %></div>
<%   }  %>
</div>
<% } %>

<form class="formstyle" method="post">

<div class="addForm">
<div class="addFormTop">
<div class="addFormCatSelect">
<div class="addFormLabel">Category</div>
<select name="categoryId">
<%
  Iterator cats = manager.getNewsCategories();
  while ( cats.hasNext() ) {
    NewsCategory cat = (NewsCategory) cats.next();
%>
    <option value="<%= cat.getId() %>"<%= categoryId == cat.getId() ? " SELECTED" : "" %>><%= cat.getName() %></option>
<%
  }
%>
</select>
</div>

<div class="addFormLabel">News Feed Name</div>
<input type="text" name="name" class="addFormInput" value="<%= name %>">

</div>

<div class="addFormRows">
<div class="addFormRow">
<div class="addFormLabel">Refresh</div>
<select name="refresh">
<% 
   String [] times = { "30", "60", "90", "120", "180", "360", "720", "1440" };
   String [] timesName = { "half hour", "1 hour", "1.5 hours", "2 hours", "3 hours", "6 hours", "12 hours", "24 hours" };
   for ( int i = 0; i < times.length; i++ ) { 
%>
    <option value="<%= times[i] %>" <%= times[i].equals( refresh ) ? "SELECTED" : "" %>><%= timesName[i] %></option>
<% } %>
</select>
</div>
<div class="addFormRow">
<div class="addFormLabel">Feed Src</div>
<input type="text" class="addFormInput" name="src" value="<%= src %>">
</div>
<div class="addFormRow">
  <div class="addFormLabel">Site URL</div>
  <input type="text" name="site" class="addFormInput" value="<%= site %>">
</div>

<div class="addFormRow">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td class="addFormCell">
  <div class="addFormLabel">Username</div>
  <input type="text" name="username" class="addFormInputSmall" value="<%= username %>">
</td><td class="addFormCell">
  <div class="addFormLabel">Password</div>
  <input type="password" name="password" class="addFormInputSmall" value="<%= password %>" >
</td></tr>
</table>
</div>

</div>

<div class="addFormBottom">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="addFormButton"><a href="javascript:document.forms[0].submit()" class="submitNewsLink"><%= isEditing ? "Edit " : "Add " %> News Feed</a></td>
<% if ( isEditing ) { %>
<td align="right" class="addFormButton"><a href="editNewsCategory.jsp?catId=<%= newsCategory.getId() %>" class="submitNewsLink">Cancel</a></td>
<% } %>
</tr></table>
</div>

</div>

<input type="hidden" name="catId" value="<%= newsCategory.getId() %>">
<input type="hidden" name="action" value="doIt">
<% if ( isEditing ) { %>
<input type="hidden" name="feedId" value="<%= feedId %>">
<% } %>

</form>

</div>

</body>

<script>

function populateSite ()
{
  var s = document.forms[0].src.value;

  re = /http:\/\/(.+)\/.+/;

  if ( !re.test( s ) )
  {
    alert( "Enter a url for the news src, bitch." );
    document.forms[0].src.focus();
  }
  else 
  {
    var n = s.replace(re, "http://$1");
    document.forms[0].site.value = n;
  }
}

document.forms[0].name.focus();

</script>

</html>